﻿using Microsoft.Extensions.Logging;
using RBAC.MaHaiBo.Entity;
using RBAC.MaHaiBo.Entity.Traceability;
using RBAC.MaHaiBo.Repository.Interface.TraceabilityInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RBAC.MaHaiBo.Service.TraceabilityService
{
    /// <summary>
    /// 溯源服务层
    /// </summary>
    public class TracingService: ITracingService
    {
        /// <summary>
        ///日志
        /// </summary>
        ILogger<TracingService> logger;
        /// <summary>
        /// 溯源仓储接口
        /// </summary>
        ITracingRepository TracingR;
        /// <summary>
        /// 商品仓储接口
        /// </summary>
        ICommodityRepository CommodityR;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="TracingR">溯源仓储接口</param>
        /// <param name="CommodityR">商品仓储接口</param>
        public TracingService(ILogger<TracingService> logger,
        ITracingRepository TracingR,
        ICommodityRepository CommodityR)
        {
            this.logger = logger;
            this.TracingR = TracingR;
            this.CommodityR = CommodityR;
        }
        /// <summary>
        /// 显示商品名称
        /// </summary>
        /// <returns>返回商品列表</returns>
       public async Task<List<Commodity>> GetCommodities()
        {
            try
            {
                return await CommodityR.GetAllAsync();
            }
            catch (Exception ex)
            {
                logger.LogError("【溯源服务层】显示商品名称 出错");
                throw;
            }
        }

        /// <summary>
        /// 添加溯源
        /// </summary>
        /// <param name="tracing">要添加的信息</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddTracing(Tracing tracing)
        {
            try
            {
                var count = await TracingR.GetAsync(p => p.LOT == tracing.LOT);
                if(count.Count>0)
                {
                    return -2;
                }

                return await TracingR.AddAsync(tracing);
            }
            catch (Exception ex)
            {
                logger.LogError("【溯源服务层】添加溯源 出错");
                throw;
            }
        }

        /// <summary>
        /// 显示溯源列表信息
        /// </summary>
        /// <param name="pageiden">页码</param>
        /// <param name="pageSzen">页容量</param>
        /// <param name="CommodityName">商品名称</param>
        /// <returns>返回数据信息</returns>
        public async Task<FenYeDTO<TracingDTO>> GetTracing(int pageiden, int pageSzen, string CommodityName)
        {
            try
            {
                var FenYe = new FenYeDTO<TracingDTO>();
                var list = (from T in await TracingR.GetAllAsync()
                            join C in await CommodityR.GetAllAsync()
                            on T.CommodityId equals C.CommodityId
                            select new TracingDTO
                            {
                                CommodityId= T.CommodityId,
                                CommodityName = C.CommodityName,
                                TracingId=T.TracingId,
                                LOT = T.LOT,
                                ShelfLife = T.ShelfLife,
                                ProductionPermit = T.ProductionPermit,
                                Manufacturer = T.Manufacturer,
                                SurfacePlot = T.SurfacePlot,
                                AddName=T.AddName,
                                AddTime = T.AddTime,
                                UpdName = T.UpdName,
                                UpdTime = T.UpdTime
                            });
                if (!string.IsNullOrEmpty(CommodityName))
                {
                    list = list.Where(p => p.CommodityName.Contains(CommodityName));
                }
                FenYe.totyCount = list.Count();
                FenYe.pageCount =(int) Math.Ceiling(FenYe.totyCount * 1.0 / pageSzen);
                FenYe.list = list.OrderBy(p=>p.TracingId).Skip((pageiden-1)* pageSzen).Take(pageSzen).ToList();
                return FenYe;
            }
            catch (Exception ex)
            {
                logger.LogError("【溯源服务层】显示溯源列表信息 出错");
                throw;
            }
        }
        /// <summary>
        /// 修改溯源
        /// </summary>
        /// <param name="tracing">效果后的溯源信息</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdTracing(Tracing tracing)
        {
            try
            {
                
                tracing.UpdName = "李四";
                tracing.UpdTime= DateTime.UtcNow;
                return await TracingR.UpdateAsync(tracing);
            }
            catch (Exception ex)
            {
                logger.LogError("【溯源服务层】修改溯源 出错");
                throw;
            }
        }
        /// <summary>
        /// 删除溯源信息
        /// </summary>
        /// <param name="Ids">要删除的数据信息集合</param>
        /// <returns>返回受影响行数</returns>
       public async Task<int> DelTracing(List<int> Ids)
        {
            try
            {
                var list = await TracingR.GetAsync(p=>Ids.Contains(p.TracingId));
                var count = 0;
                foreach (var item in list)
                {
                    count= await TracingR.DeleteAsync(item);
                }
               return count;

            }
            catch (Exception ex)
            {
                logger.LogError("【溯源服务层】修改溯源 出错");
                throw;
            }
        }
        /// <summary>
        /// 反填数据信息
        /// </summary>
        /// <param name="Tracingid">编号</param>
        /// <returns>返回数据信息</returns>
        public async Task<List<TracingDTO>> Tracinginfo(int Tracingid)
        {
            try
            {
                var list = (from T in await TracingR.GetAllAsync()
                            join C in await CommodityR.GetAllAsync()
                            on T.CommodityId equals C.CommodityId
                            where T.TracingId == Tracingid
                            select new TracingDTO
                            {
                                CommodityId = T.CommodityId,
                                CommodityName = C.CommodityName,
                                TracingId = T.TracingId,
                                LOT = T.LOT,
                                ShelfLife = T.ShelfLife,
                                ProductionPermit = T.ProductionPermit,
                                Manufacturer = T.Manufacturer,
                                SurfacePlot = T.SurfacePlot,
                                AddName = T.AddName,
                                AddTime = T.AddTime,
                                UpdName = T.UpdName,
                                UpdTime = T.UpdTime
                            });
                return list.ToList();
            }
            catch (Exception ex)
            {
                logger.LogError("【溯源服务层】修改溯源 出错");
                throw;
            }
        }

       
    }
}
